import seal from '@/lib/request/seal'
import { RootState } from '../index'
import { Commit } from 'vuex'
import { IResponse } from '@/lib/service'

export interface AppliedForMeState {
  sealList: any[]
  total: number
  detailVisible: boolean
  loading: boolean
  sealData?: any
  revokeVisible: boolean
  recoverVisible: boolean
  lossVisible: boolean
}
const initState: AppliedForMeState = {
  sealList: [],
  total: 0,
  detailVisible: false,
  loading: false,
  sealData: null,
  revokeVisible: false,
  recoverVisible: false,
  lossVisible: false,
}

const getters = {}

const actions = {
  async getList(
    context: {
      commit: Commit
      state: AppliedForMeState
      rootState: RootState
      getters: any
    },
    payload: any
  ) {
    context.commit('setStateData', { loading: true })
    const data: IResponse = await seal
      .sealList(
        payload.pageIndex,
        payload.pageSize,
        payload.sealName,
        payload.status,
        payload.sealType
      )
      .catch((e: any) => {
        context.commit('setStateData', {
          loading: false,
          sealList: [],
          total: 0,
        })
      })
    context.commit('setStateData', { loading: false })
    if (data && data.data) {
      context.commit('setStateData', {
        sealList: data.data.list || [],
        total: data.data.total,
      })
    }
  },
  async sealDetail(
    context: {
      commit: Commit
      state: AppliedForMeState
      rootState: RootState
      getters: any
    },
    payload: string
  ) {
    const data: IResponse = await seal.sealDetail(payload)

    return data.data
  },
}

const mutations = {
  setStateData(state: AppliedForMeState, data: any) {
    // eslint-disable-next-line no-param-reassign
    state = Object.assign(state, data)
  },
}

export default {
  namespaced: true,
  state: initState,
  getters,
  actions,
  mutations,
}
